﻿using Contrib.BSNavigation.Models;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;

namespace Contrib.BSNavigation.Drivers {
    [UsedImplicitly]
    public class DataSectionLinkPartDriver : ContentPartDriver<DataSectionLinkPart> {
        
        // Best practice: always specify a Prefix to avoid potential input name conflicts
        protected override string Prefix {
            get { return "DataSectionLink"; }
        }

        protected override DriverResult Editor(DataSectionLinkPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_DataSectionLink_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/DataSectionLink", Model: part, Prefix: Prefix));
        }

        protected override DriverResult Editor(DataSectionLinkPart part, IUpdateModel updater, dynamic shapeHelper) {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }

        // Add support for exporting our part
        protected override void Exporting(DataSectionLinkPart part, ExportContentContext context) {
            context.Element(part.PartDefinition.Name).SetAttributeValue("DataSectionValue", part.DataSectionValue);
        }

        // Add support for importing our part
        protected override void Importing(DataSectionLinkPart part, ImportContentContext context) {
            context.ImportAttribute(part.PartDefinition.Name, "DataSectionValue", x => part.DataSectionValue = x);
        }

    }
}